<?php

namespace Libraries\OAuth2Client;
use Models\WechatMpAccessToken;
use RunTimeException;

class WechatMpOAuth2Client extends OAuth2Client
{
    public function redirectToAuthorize($scopes)
    {

    }

    public function getAccessToken($code = null)
    {
        $config = CONFIG('wechat.mp');
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$config['appId']}&secret={$config['appSecret']}";
        $result = curl_get($url);
        $response = json_decode($result);
        if (isset($response->access_token)) {
            $access_token = new WechatMpAccessToken;
            $access_token['access_token'] = $response->access_token;
            $access_token['expire_at'] = date('Y-m-d H:i:s', time()+$response->expires_in);
            $access_token->add();

            return $access_token;
        } else {
            throw new RunTimeException("get access_token error".$response->errmsg, $response->errcode);
        }
    }

    public function refreshToken($refresh_token){}

    public function getUserInfo($access_token){}
}